package openjdk.stream;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * Created by Asin Liu on 2017/2/21.
 */
public class StreamMain {
    public static void main(String[] args) {
        BigDecimal bd1 = new BigDecimal("0");
        BigDecimal bdz = BigDecimal.ZERO;
        System.out.println(bd1.equals(bdz));
        System.out.println(bd1 == bdz);


        // int 累加
        int test1 = Stream.of(1, 2, 3, 4)
                .filter((i) -> i > 1)
                .reduce((result, filed) -> result + filed).get();

        // String 累加
        String test2 = Stream.of("He", "llo", " ", "Wo", "rld")
                .reduce((result, filed) -> result + filed)
                .get();

        System.out.println(test1);
        System.out.println(test2);

        List<String> lst = new ArrayList<>();

        for (int i = 0; i < 5; i++) {
            lst.add("_" + i);
        }

        String cc = lst.stream().reduce((r, f) -> r + "," + f).get();
        System.out.printf("souf: \t" + cc);

        System.out.println();
        String[] tags = {"a", "b", "c", "d"};


        String strTags = "";
        for (int i = 0; i < tags.length; i++) {
            strTags += tags[i];
            strTags += ",";
        }

        strTags = strTags.substring(0, strTags.length() - 1);
        System.out.println(strTags);
        System.out.println(Arrays.stream(tags).reduce((r, f) -> r + "," + f));

        List<X> xlst = new ArrayList<X>() {{
            add(new X(1, BigDecimal.TEN));
            add(new X(2, BigDecimal.TEN));
            add(new X(3, BigDecimal.ONE));
            add(new X(3, BigDecimal.ONE));
            add(new X(3, BigDecimal.ONE));
        }};


        BigDecimal in6h = xlst.stream()
                .filter(o -> o.getId() > 0)
                .map(o -> o.getQuantity())
                .reduce((m, n) -> m.add(n)).get();

        System.out.println(in6h);

    }

    static class X {
        int id;
        BigDecimal quantity;

        public X(int id, BigDecimal quantity) {
            this.id = id;
            this.quantity = quantity;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public BigDecimal getQuantity() {
            return quantity;
        }

        public void setQuantity(BigDecimal quantity) {
            this.quantity = quantity;
        }
    }
}
